package com.osce.web.controller.system;

import com.osce.common.annotation.Log;
import com.osce.common.constant.UserConstants;
import com.osce.common.core.controller.BaseController;
import com.osce.common.core.entity.AjaxResult;
import com.osce.common.core.entity.entity.SysUser;
import com.osce.common.core.entity.vo.SimulatedPatientExcelVo;
import com.osce.common.core.page.TableDataInfo;
import com.osce.common.enums.BusinessType;
import com.osce.common.utils.SecurityUtils;
import com.osce.common.utils.StringUtils;
import com.osce.common.utils.poi.ExcelUtil;
import com.osce.interfaces.system.ISimulatedPatientService;
import com.osce.interfaces.system.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 标准化病人
 *
 * @author doudou
 */
@RestController
@RequestMapping("/system/simulated-patient")
public class SimulatedPatientController extends BaseController {

    @Autowired
    private ISimulatedPatientService simulatedPatientService;

    @Autowired
    private ISysUserService userService;


    /**
     * 获取标准化病人列表
     */
    @PreAuthorize("@ss.hasPermi('system:simulated_patient:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUser user) {
        user.setUserType(UserConstants.SIMULATED_PATIENT_TYPE);
        startPage();
        List<SysUser> list = simulatedPatientService.selectList(user);
        return getDataTable(list);
    }

    /**
     * 新增标准化病人
     */
    @PreAuthorize("@ss.hasPermi('system:simulated_patient:add')")
    @Log(title = "标准化病人管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysUser user) {
        if (!userService.checkUserNameUnique(user)) {
            return error("新增标准化病人'" + user.getUserName() + "'失败，登录账号已存在");
        } else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) {
            return error("新增标准化病人'" + user.getUserName() + "'失败，手机号码已存在");
        } else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user)) {
            return error("新增标准化病人'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        user.setCreateBy(getUsername());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        user.setUserType(UserConstants.SIMULATED_PATIENT_TYPE);
        user.setDeptId(UserConstants.SIMULATED_PATIENT_DICT);
        user.setRoleIds(UserConstants.SIMULATED_PATIENT_ROLES);
        return toAjax(userService.insertUser(user));
    }

    /**
     * 导出模板
     */
    @PostMapping("/importTemplate")
    @PreAuthorize("@ss.hasPermi('system:simulated_patient:import')")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SimulatedPatientExcelVo> util = new ExcelUtil<>(SimulatedPatientExcelVo.class);
        util.importTemplateExcel(response, "标准化病人数据");
    }

    @Log(title = "导出标准化病人列表", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:simulated_patient:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUser user) {
        user.setUserType(UserConstants.SIMULATED_PATIENT_TYPE);
        List<SysUser> list = simulatedPatientService.selectList(user);
        List<SimulatedPatientExcelVo> userList = simulatedPatientService.toExcelVo(list);
        ExcelUtil<SimulatedPatientExcelVo> util = new ExcelUtil<>(SimulatedPatientExcelVo.class);
        util.exportExcel(response, userList, "标准化病人数据");
    }

    /**
     * 导入标准化病人
     */
    @Log(title = "导入标准化病人", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:simulated_patient:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SimulatedPatientExcelVo> util = new ExcelUtil<>(SimulatedPatientExcelVo.class);
        List<SimulatedPatientExcelVo> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = simulatedPatientService.importSimulatedPatient(userList, updateSupport, operName);
        return success(message);
    }
}
